<template>
  <div>
    <!-- 查询表单 -->
    <div class="check-form-box">
      <el-form
        :inline="true"
        :model="searchModel.data"
        class="demo-form-inline"
      >
        <el-form-item label="创建日期" class="timeForm">
          <el-date-picker
            v-model="searchModel.createDate"
            type="daterange"
            range-separator="-"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item label="结算日期" class="timeForm">
          <el-date-picker
            v-model="searchModel.settleDate"
            type="daterange"
            range-separator="-"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item label="用户ID">
          <el-input
            v-model.trim="searchModel.userId"
            :formatter="(value) => value.replace(/[^\d]/g, '')"
            placeholder="请输入用户ID"
          />
        </el-form-item>
        <el-form-item label="订单状态">
          <el-select v-model="searchModel.orderStatus" placeholder="请选择">
            <el-option label="全部" :value="null" />
            <el-option label="已付款" value="2" />
            <el-option label="已结算" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" color="#ea5b24" @click="searchOrder()"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <!-- 列表 -->
    <div>
      <el-table :data="tableData.data" style="width: 100%">
        <el-table-column prop="yzCreateTimeStr" width="160" label="创建时间" />
        <el-table-column prop="orderNo" width="180" label="订单编号" />
        <el-table-column prop="goodsName" width="180" label="订单标题" />
        <el-table-column prop="orderType" label="订单类型" />
        <el-table-column prop="paidAmount" label="支付金额" />
        <el-table-column prop="estimateAmount" label="预估佣金" />
        <el-table-column prop="ratio" label="佣金比例" />
        <el-table-column prop="paidTimeStr" width="160" label="支付时间" />
        <el-table-column prop="settleTimeStr" width="160" label="结算时间" />
        <el-table-column prop="settleAmount" label="结算佣金" />
        <el-table-column prop="yzUpdateTimeStr" width="160" label="更新时间" />
        <el-table-column prop="userId" label="用户ID" />
        <!-- <el-table-column prop="" label="返用户比例" /> -->
        <el-table-column prop="rewardGold" width="100" label="返用户金额" />
        <el-table-column
          prop="orderStatus"
          :formatter="judgeOrderStatus"
          label="订单状态"
        />
      </el-table>
      <!-- 分页插件 -->
      <div style="text-align: center">
        <el-pagination
          :total="totalNum"
          @current-change="handleCurrentChange"
          background
          :page-size="size"
          layout="prev, pager, next, total"
          :current-page="page"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import { findYunZhanOrderApi } from "../../../api/index";
import { formatDateTime } from "../../../utils/formatDateTime";

// 分页列表数据
let tableData: any = reactive([]);
const totalNum = ref(0);
const page = ref(1);
const size = 8;

// 翻页
let flipData = reactive({
  page: page,
  size: size,
});
const handleCurrentChange = (val: number) => {
  page.value = val;
  flipData.page = page.value;
  findOrder(flipData);
};

// 搜索模型
const searchModel: any = reactive({
  createDate: null,
  startCreateTime: null,
  endCreateTime: null,
  settleDate: null,
  startSettleTime: null,
  endSettleTime: null,
  userId: null,
  orderStatus: null,
  page: page,
  size: size,
});

// 过滤订单状态
const judgeOrderStatus = (row) => {
  switch (row.orderStatus) {
    case 1:
      return "已失效";
    case 2:
      return "已付款";
    case 3:
      return "已结算";
    default:
      return "";
  }
};

// 搜索通行订单
const searchOrder = () => {
  page.value = 1;
  if (searchModel.createDate) {
    searchModel.startCreateTime = formatDateTime(searchModel.createDate[0]);
    searchModel.endCreateTime = formatDateTime(
      searchModel.createDate[1]
    ).replace("00:00:00", "23:59:59");
  } else {
    searchModel.startCreateTime = null;
    searchModel.endCreateTime = null;
  }
  if (searchModel.settleDate) {
    searchModel.startSettleTime = formatDateTime(searchModel.settleDate[0]);
    searchModel.endSettleTime = formatDateTime(
      searchModel.settleDate[1]
    ).replace("00:00:00", "23:59:59");
  } else {
    searchModel.startSettleTime = null;
    searchModel.endSettleTime = null;
  }
  searchParmSearch();
  findOrder(searchModel);
  flipData = JSON.parse(JSON.stringify(searchModel));
};

// 查询通行订单
const findOrder = (searchModel) => {
  findYunZhanOrderApi(searchModel).then((res) => {
    if (res.data.code === 2000) {
      tableData.data = res.data.data.data;
      totalNum.value = res.data.data.total;
    } else {
      ElMessage.error("查询失败");
    }
  });
};

// 搜索参数检查
const searchParmSearch = () => {
  Object.keys(searchModel).forEach((key) => {
    if (!searchModel[key]) {
      searchModel[key] = null;
    }
  });
};

onMounted(() => {
  findOrder(searchModel);
});
</script>

<style scoped>
.timeForm {
  width: 20% !important;
}

:deep(.el-form-item) {
  margin-right: 2%;
}
</style>
